package com.rongke.web.gubo;

import com.alibaba.fastjson.JSONObject;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.methods.RequestBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.web.bind.annotation.CrossOrigin;

import java.io.IOException;

/**
 * Created by Administrator on 2017/8/2 0002.
 */
@CrossOrigin
public class RoyaltyApi{
    public static JSONObject getToken() throws IOException {
        CloseableHttpClient client = HttpClients.createDefault();
        String reqUrl = "https://api.360qiquan.com/api/auth/access_token";
        HttpUriRequest request = RequestBuilder.post()//get请求
                .setUri(reqUrl)
               /* .addHeader(APIX_KEY, apixKey)*/
                .addParameter("username","rongke")
                .build();
        CloseableHttpResponse response = client.execute(request);
        //将字符串转换成jsonObject对象
        JSONObject myJsonObject = new JSONObject();
        myJsonObject=myJsonObject.parseObject( EntityUtils.toString(response.getEntity()));
        return myJsonObject;
    }

    public static JSONObject getPrecent(String code,String cycle,String token) throws IOException {
        CloseableHttpClient client = HttpClients.createDefault();
        String reqUrl = "https://api.360qiquan.com/api/v1/option/premium_rate";
        HttpUriRequest request = RequestBuilder.get()//get请求
                .setUri(reqUrl)
                .addHeader("Authorization", "Bearer"+" "+token)
                .addParameter("stock_code",code)
                .addParameter("cycle",cycle)
                .build();
        CloseableHttpResponse response = client.execute(request);
        JSONObject myJsonObject = new JSONObject();
        myJsonObject=myJsonObject.parseObject( EntityUtils.toString(response.getEntity()));
        return myJsonObject;
    }

    public static String  selectSharePage(String code) throws IOException {
        CloseableHttpClient client = HttpClients.createDefault();
        StringBuffer reqUrl = new StringBuffer("http://qt.gtimg.cn");
        HttpUriRequest request = RequestBuilder.get()//get请求
                .setUri(reqUrl.append(code).toString())
                .addParameter("q",code)
                .build();
        CloseableHttpResponse response = client.execute(request);
        String result = EntityUtils.toString(response.getEntity());
        return result;
    }



   public static void main(String[] args) throws IOException {
        System.out.println(getToken());
    }
}
